package com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.bw.liuyushan.mvp_lib.View.BaseFragment;
import com.bw.liuyushan.zhuangao4_mvptest1.Entity.UserBean;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Contrack.ICTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Activity.MainActivity;
import com.bw.liuyushan.zhuangao4_mvptest1.R;
import com.bw.liuyushan.zhuangao4_mvptest1.Utils.WindowUtils;

import static android.content.Context.MODE_PRIVATE;

/**
 * ZhuanGao4_MVPTest1
 * file name is : logFragment
 * created by Ender on 2021/8/19 13:37.
 * author : 刘雨杉
 * Describe(作用):登录的Fragment 归属于UserActivity 登录 传值 跳转
 */
public class logFragment extends BaseFragment<BasePresenterTest> implements ICTest.C_ViewTest {
    private android.widget.EditText name;
    private android.widget.EditText pass;
    private android.widget.Button log;

    @Override
    public int bindLayout() {
        return R.layout.activity_loginin;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip = new BasePresenterTest(new BaseModelTest(), this);

        name = (EditText) findViewById(R.id.name);
        pass = (EditText) findViewById(R.id.pass);
        log = (Button) findViewById(R.id.log);
    }

    @Override
    public void initDate() {
        log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String logname = name.getText().toString();
                String logpass = pass.getText().toString();
                ip.mpLogin(logname,logpass);
            }
        });
    }

    /**
     * 登录成功->sp的编辑者提交事务->传递布尔值isLogin->后续判断true则是已登录状态false则是未登录
     * @param userBean
     */
    @Override
    public void ShowLogin(UserBean userBean) {
        if (userBean.getCode()==0){
            ToastUtils.showShort("欢迎你--"+userBean.getData().getUserName());
            //创建sp对象
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("sp",MODE_PRIVATE);
            SharedPreferences.Editor edit = sharedPreferences.edit();//获取编辑者
            edit.putString("LoginUser",userBean.getData().getUserName().toString());//传递登陆用户名
            edit.putBoolean("isLogin",true);//传递布尔值
            edit.commit();//提交事务
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }else{
            ToastUtils.showShort("登录失败,信息有误");
        }
    }

}
